package 课设;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class login extends JFrame {
    private JTextField userName;
    private JPasswordField password;
    private JButton user_login,register,manager_login;
    public login() {
        super();
        this.setSize(350,250);
        this.setTitle("Login");
        this.setLocationRelativeTo(getOwner());//居中

        //设置组合布局———3行2列表格
        Container cont=getContentPane();
        cont.setLayout(new GridLayout(4,2));

        //添加“用户名”、“密码”
        cont.add(new JLabel("username"));
        userName = new JTextField(10);
        cont.add(userName);
        cont.add(new JLabel("password"));
        password =new JPasswordField(10);
        cont.add(password);

        //添加“登录”、“注册”按钮
        user_login=new JButton("用户登录");
        register=new JButton ("注册");
        manager_login = new JButton("管理员登录");
        cont.add(user_login);
        cont.add(register);
        cont.add(manager_login);

        db dbcon = new db();

        manager_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String pass = new String(password.getPassword());
                if(authenticateManager(userName.getText(), pass)) {
                    manager_show hello = new manager_show();
                    hello.setVisible(true);
                    dispose();
                }else {
                    JOptionPane.showMessageDialog(null, "你输入的密码没有通过验证");
                }
            }
        });
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userAdd w = new userAdd("");
                w.setVisible(true);
            }
        });
        //注册监听器
        user_login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                String pass = new String(password.getPassword());
                if(authenticateUser(userName.getText(), pass)) {
                    blog_show hello = new blog_show(userName.getText());
                    hello.setVisible(true);
                    dispose();
                }else {
                    JOptionPane.showMessageDialog(null, "你输入的密码没有通过验证");
                }
            }
        });
    }
    private boolean authenticateManager(String username, String password){
        try {
            // 连接数据库
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=blog_controller;trustServerCertificate=true;", "sa", "123456");

            // 查询数据库中是否存在匹配的用户名和密码
            String query = "SELECT * FROM manager WHERE manager = ? AND pwd = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, username);
                preparedStatement.setString(2, password);
                ResultSet resultSet = preparedStatement.executeQuery();

                // 如果查询结果非空，说明用户名和密码匹配成功
                if (resultSet.next()) {
                    // 显示登录成功的信息窗口
                    JOptionPane.showMessageDialog(null, "登录成功！");
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    private boolean authenticateUser(String username, String password) {
        try {
            // 连接数据库
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=blog_controller;trustServerCertificate=true;", "sa", "123456");

            // 查询数据库中是否存在匹配的用户名和密码
            String query = "SELECT * FROM users WHERE username = ? AND password = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, username);
                preparedStatement.setString(2, password);
                ResultSet resultSet = preparedStatement.executeQuery();

                // 如果查询结果非空，说明用户名和密码匹配成功
                if (resultSet.next()) {
                    // 显示登录成功的信息窗口
                    JOptionPane.showMessageDialog(null, "登录成功！");
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static void main(String[] args) {
        login w = new login();
        w.setVisible(true);
    }
}